package com.tencent.wepay.protocol.pay.request;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import com.tencent.wepay.constant.enums.TradeTypeEnum;
import com.tencent.wepay.util.DateUtil;
import com.tencent.wepay.util.RandomUtil;
import com.tencent.wepay.util.SignUtil;

public class PrepayRequest implements Serializable {
	private static final long serialVersionUID = 3008511056484900724L;
	private  String appid;
	private  String mchId;
	private  String prepayid;
	private  String packageVar;
	private  String nonceStr;
	private  String timestamp;
	private  String sign;
	private  TradeTypeEnum traceType;

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getPrepayid() {
		return prepayid;
	}

	public void setPrepayid(String prepayid) {
		this.prepayid = prepayid;
	}

	public String getPackageVar() {
		return packageVar;
	}

	public void setPackageVar(String packageVar) {
		this.packageVar = packageVar;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public TradeTypeEnum getTraceType() {
		return traceType;
	}

	public void setTraceType(TradeTypeEnum traceType) {
		this.traceType = traceType;
	}

	public static class Builder {
		private String appid;
		private String mchId;
		private String prepayid;
		private String packageVar;
		private String nonceStr;
		private String timestamp;
		private String sign;
		private String key;
		private TradeTypeEnum traceType;
		// 构建的步骤
		public Builder(TradeTypeEnum traceType) {
			this.traceType = traceType;
		}
		
		public Builder appid(String appid) {
			this.appid = appid;
			return this;
		}

		public Builder mchId(String mchId) {
			this.mchId = mchId;
			return this;
		}

		public Builder prepayid(String prepayid) {
			this.prepayid = prepayid;
			return this;
		}
		
		public Builder key(String key) {
			this.key = key;
			return this;
		}
		
		public PrepayRequest build() { // 构建，返回一个新对象
			Map<String, Object> params = new TreeMap<String, Object>(
				new Comparator<String>() {
				     public int compare(String obj1, String obj2) {
				          // 降序排序
				          return obj1.compareTo(obj2);
				     }
				 }
			);	
			this.nonceStr=RandomUtil.uuid();
			this.timestamp=DateUtil.getSeconds()+"";
			if(this.traceType.equals(TradeTypeEnum.APP)){
				this.packageVar="Sign=WXPay";
				params.put("appid", this.appid);
				params.put("partnerid", this.mchId);
				params.put("prepayid", this.prepayid);
				params.put("package", this.packageVar);
				params.put("noncestr", this.nonceStr);
				params.put("timestamp", this.timestamp);
			}
			else if(this.traceType.equals(TradeTypeEnum.JSAPI)){
				this.packageVar="prepay_id="+this.prepayid;
				params.put("appId", this.appid);
				params.put("package", this.packageVar);
				params.put("nonceStr", this.nonceStr);
				params.put("signType", "MD5");
				params.put("timeStamp", this.timestamp);
			}
			this.sign=SignUtil.toSign(params, key);
			return new PrepayRequest(this);
		}
	}

	private PrepayRequest(Builder builder) {
		appid=builder.appid;
		mchId=builder.mchId;
		nonceStr=builder.nonceStr;
		packageVar=builder.packageVar;
		prepayid=builder.prepayid;
		sign=builder.sign;
		timestamp=builder.timestamp;
		traceType=builder.traceType;
	}
}
